package com.wonders.hs.pd.mm.web.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.wonders.hs.pd.mm.web.config.SystemConfig;
import com.wonders.hs.pd.mm.web.utils.ExcelFileGenerator;
import com.wonders.hs.pd.mm.web.utils.HttpRequestUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.*;

import static com.wonders.hs.pd.mm.web.utils.ExcelFileGenerator.buildExcelDocument;

/**
 * @author banglong
 * @date 2016/10/9
 * web程序的几张报表的导出功能
 */
@Controller
@RequestMapping("/export")
public class ExcelController {

  public static final int EXCEL_TYPE_COMMON = 0;
  public static final int EXCEL_TYPE_SINGLE_DEPTAPPLY = 1;
  public static final int EXCEL_TYPE_GOODSBOOK = 2;
  public static final int EXCEL_TYPE_PACKAGEDRAW = 3;
  public static final int EXCEL_TYPE_CARDLISTINFO = 4;

  @RequestMapping(value = "/stockExcel")
  public void stockExcel(String name,
                         String className,
                         String method,
                         String goodsSmallCatID,
                         String goodsMidCatID,
                         String deptAccountID,
                         String goodsID,
                         String goodsBookID,
                         String goodsBigCatID,
                         String fieldName,
                         String fieldValueName,
                         HttpServletResponse response) {

    try {
      name = URLDecoder.decode(name, "utf-8");
      className = URLDecoder.decode(className, "utf-8");
      method = URLDecoder.decode(method, "utf-8");
      goodsSmallCatID = URLDecoder.decode(goodsSmallCatID, "utf-8");
      goodsMidCatID = URLDecoder.decode(goodsMidCatID, "utf-8");
      deptAccountID = URLDecoder.decode(deptAccountID, "utf-8");
      goodsID = URLDecoder.decode(goodsID, "utf-8");
      goodsBookID = URLDecoder.decode(goodsBookID, "utf-8");
      goodsBigCatID = URLDecoder.decode(goodsBigCatID, "utf-8");
      fieldName = URLDecoder.decode(fieldName, "utf-8");
      fieldValueName = URLDecoder.decode(fieldValueName, "utf-8");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }

    String url = SystemConfig.API_ADDRESS + className + "/" + method; // 访问地址
    Map<String, Object> map = new HashMap<>();
    map.put("goodsSmallCatID", goodsSmallCatID);
    map.put("goodsMidCatID", goodsMidCatID);
    map.put("deptAccountID", deptAccountID);
    map.put("goodsID", goodsID);
    map.put("goodsBookID", goodsBookID);
    map.put("goodsBigCatID", goodsBigCatID);

    String result = HttpRequestUtils.sendPost(url, map);

    exportExcel(name, fieldName, fieldValueName, response, result, EXCEL_TYPE_COMMON);
  }

  @RequestMapping(value = "/singleDeptApply")
  public void singleDeptApply(String name,
                              String className,
                              String method,
                              String providerDeptAccountID,
                              String applyDeptID,
                              String startTime,
                              String endTime,
                              String fieldName,
                              String fieldValueName,
                              HttpServletResponse response) {

    try {
      name = URLDecoder.decode(name, "utf-8");
      className = URLDecoder.decode(className, "utf-8");
      method = URLDecoder.decode(method, "utf-8");
      providerDeptAccountID = URLDecoder.decode(providerDeptAccountID, "utf-8");
      applyDeptID = URLDecoder.decode(applyDeptID, "utf-8");
      startTime = URLDecoder.decode(startTime, "utf-8");
      endTime = URLDecoder.decode(endTime, "utf-8");
      fieldName = URLDecoder.decode(fieldName, "utf-8");
      fieldValueName = URLDecoder.decode(fieldValueName, "utf-8");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }

    String url = SystemConfig.API_ADDRESS + className + "/" + method; // 访问地址
    Map<String, Object> map = new HashMap<>();
    map.put("providerDeptAccountID", providerDeptAccountID);
    map.put("applyDeptID", applyDeptID);
    map.put("startTime", startTime);
    map.put("endTime", endTime);

    String result = HttpRequestUtils.sendPost(url, map);

    exportExcel(name, fieldName, fieldValueName, response, result, EXCEL_TYPE_SINGLE_DEPTAPPLY);
  }

  @RequestMapping(value = "/goodsBookExcel")
  public void goodsBookExcel(String name,
                             String className,
                             String method,
                             String providerDeptAccountID,
                             String deptAccountID,
                             String goodsBookID,
                             String startTime,
                             String endTime,
                             String fieldName,
                             String fieldValueName,
                             HttpServletResponse response) {

    try {
      name = URLDecoder.decode(name, "utf-8");
      className = URLDecoder.decode(className, "utf-8");
      method = URLDecoder.decode(method, "utf-8");
      providerDeptAccountID = URLDecoder.decode(providerDeptAccountID, "utf-8");
      startTime = URLDecoder.decode(startTime, "utf-8");
      endTime = URLDecoder.decode(endTime, "utf-8");
      fieldName = URLDecoder.decode(fieldName, "utf-8");
      fieldValueName = URLDecoder.decode(fieldValueName, "utf-8");
      deptAccountID = URLDecoder.decode(deptAccountID, "utf-8");
      goodsBookID = URLDecoder.decode(goodsBookID, "utf-8");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }

    String url = SystemConfig.API_ADDRESS + className + "/" + method; // 访问地址
    Map<String, Object> map = new HashMap<>();
    map.put("providerDeptAccountID", providerDeptAccountID);
    map.put("deptAccountID", deptAccountID);
    map.put("goodsBookID", goodsBookID);
    map.put("startTime", startTime);
    map.put("endTime", endTime);

    String result = HttpRequestUtils.sendPost(url, map);

    exportExcel(name, fieldName, fieldValueName, response, result, EXCEL_TYPE_GOODSBOOK);
  }

  @RequestMapping(value = "/goodsPackageDrawQueryExcel")
  public void goodsPackageDrawQueryExcel(String name,
                                         String className,
                                         String method,
                                         String startTime,
                                         String endTime,
                                         String packageID,
                                         String packageBigID,
                                         String deptID,
                                         String goodsNo,
                                         String fieldName,
                                         String fieldValueName,
                                         HttpServletResponse response) {

    try {
      name = URLDecoder.decode(name, "utf-8");
      className = URLDecoder.decode(className, "utf-8");
      method = URLDecoder.decode(method, "utf-8");
      startTime = URLDecoder.decode(startTime, "utf-8");
      endTime = URLDecoder.decode(endTime, "utf-8");
      fieldName = URLDecoder.decode(fieldName, "utf-8");
      fieldValueName = URLDecoder.decode(fieldValueName, "utf-8");
      packageID = URLDecoder.decode(packageID, "utf-8");
      packageBigID = URLDecoder.decode(packageBigID, "utf-8");
      deptID = URLDecoder.decode(deptID, "utf-8");
      goodsNo = URLDecoder.decode(goodsNo, "utf-8");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }

    String url = SystemConfig.API_ADDRESS + className + "/" + method; // 访问地址
    Map<String, Object> map = new HashMap<>();
    map.put("startDate", startTime);
    map.put("endDate", endTime);
    map.put("packageID", packageID);
    map.put("packageBigID", packageBigID);
    map.put("deptID", deptID);
    map.put("goodsNo", goodsNo);

    String result = HttpRequestUtils.sendPost(url, map);

    exportExcel(name, fieldName, fieldValueName, response, result, EXCEL_TYPE_PACKAGEDRAW);
  }

  @RequestMapping(value = "/goodsPackageDrawQueryDetailExcel")
  public void goodsPackageDrawQueryDetailExcel(String name,
                                               String className,
                                               String method,
                                               String deptID,
                                               String endTime,
                                               String packageID,
                                               String startTime,
                                               String fieldName,
                                               String fieldValueName,
                                               HttpServletResponse response) {

    try {
      name = URLDecoder.decode(name, "utf-8");
      className = URLDecoder.decode(className, "utf-8");
      method = URLDecoder.decode(method, "utf-8");
      endTime = URLDecoder.decode(endTime, "utf-8");
      startTime = URLDecoder.decode(startTime, "utf-8");
      deptID = URLDecoder.decode(deptID, "utf-8");
      packageID = URLDecoder.decode(packageID, "utf-8");
      fieldName = URLDecoder.decode(fieldName, "utf-8");
      fieldValueName = URLDecoder.decode(fieldValueName, "utf-8");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }

    String url = SystemConfig.API_ADDRESS + className + "/" + method; // 访问地址
    Map<String, Object> map = new HashMap<>();
    map.put("endDate", endTime);
    map.put("startDate", startTime);
    map.put("deptID", deptID);
    map.put("packageID", packageID);

    String result = HttpRequestUtils.sendPost(url, map);
    exportExcel(name, fieldName, fieldValueName, response, result, EXCEL_TYPE_COMMON);
  }

  @RequestMapping(value = "/rtstoerQueryDetail")
  public void rtstoerQueryDetail(String name,
                                 String className,
                                 String method,
                                 String packageID,
                                 String deptAccountID,
                                 String fieldName,
                                 String fieldValueName,
                                 HttpServletResponse response) {

    try {
      name = URLDecoder.decode(name, "utf-8");
      className = URLDecoder.decode(className, "utf-8");
      method = URLDecoder.decode(method, "utf-8");
      packageID = URLDecoder.decode(packageID, "utf-8");
      deptAccountID = URLDecoder.decode(deptAccountID, "utf-8");
      fieldName = URLDecoder.decode(fieldName, "utf-8");
      fieldValueName = URLDecoder.decode(fieldValueName, "utf-8");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }

    String url = SystemConfig.API_ADDRESS + className + "/" + method; // 访问地址
    Map<String, Object> map = new HashMap<>();
    map.put("showZeroBatch", false);
    map.put("deptAccountID", deptAccountID);
    map.put("packageID", packageID);

    String result = HttpRequestUtils.sendPost(url, map);
    exportExcel(name, fieldName, fieldValueName, response, result, EXCEL_TYPE_COMMON);
  }

  /**
   * 固定资产 excel 导出
   * @param name
   * @param className
   * @param method
   * @param condition
   * @param fieldName
   * @param fieldValueName
   * @param response
   */
 @RequestMapping(value = "/cardListInfo")
  public void cardListInfo(String name,
                           String className,
                           String method,
                           String condition,
                           String StartTime,
                           String EndTime,
                           Boolean IsAttachment,
                           String fieldName,
                           String fieldValueName,
                           HttpServletResponse response) {

    try {
      name = URLDecoder.decode(name, "utf-8");
      className = URLDecoder.decode(className, "utf-8");
      method = URLDecoder.decode(method, "utf-8");
      condition = URLDecoder.decode(condition, "utf-8");
      StartTime = URLDecoder.decode(StartTime, "utf-8");
      EndTime = URLDecoder.decode(EndTime, "utf-8");
      fieldName = URLDecoder.decode(fieldName, "utf-8");
      fieldValueName = URLDecoder.decode(fieldValueName, "utf-8");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }

    String url = SystemConfig.API_ADDRESS + className + "/" + method; // 访问地址
    Map<String, Object> map = new HashMap<>();
    map.put("condition", condition);
    map.put("StartTime", StartTime);
    map.put("EndTime", EndTime);
    map.put("IsAttachment", IsAttachment);

    String result = HttpRequestUtils.sendPost(url, map);
    exportExcel(name, fieldName, fieldValueName, response, result, EXCEL_TYPE_CARDLISTINFO);
  }


  /**
   * @description 导出excel文件
   */
  private void exportExcel(String name, String fieldName, String fieldValueName, HttpServletResponse response, String result, int type) {
    Map<String, Object> page_div = new HashMap<>();
    ObjectMapper mapper = new ObjectMapper();
    try {
      Map<String, Object> jsonData = mapper.readValue(result, Map.class);
      if (Integer.parseInt(jsonData.get("issuccess").toString()) == 0) {
        page_div.put("name", name);
        result = jsonData.get("result").toString().replaceAll("\n", "");
        page_div.put("data", mapper.readValue(result, List.class));
      } else {
        page_div.put("name", "excel导出失败");
        page_div.put("data", new ArrayList<>());
      }
      buildExcelDocument(page_div, response, fieldName, fieldValueName, type);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
